package cn.tedu.vod.controller;

import cn.tedu.commonUtils.R;
import cn.tedu.serviceBase.exception.EducationException;
import cn.tedu.vod.service.VodService;
import cn.tedu.vod.utils.AliyunVodSDKUtils;
import cn.tedu.vod.utils.VodConstant;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/eduvod/vod")
@Api(tags = "视屏Api")
public class VodController {
    @Autowired
    private VodService vodService;

    @PostMapping("/upload")
    @ApiOperation("上传视频")
    public R uploadVideo(MultipartFile file){
        String videoId = vodService.uploadVideo(file);
        if (videoId.isEmpty() || videoId ==null) {
            return R.error().msg("视频上传失败");
        }
        return R.ok().data("videoId",videoId);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除视频")
    public R removeVideo(@ApiParam(name = "id", value = "云端视频id", required = true)
                         @PathVariable String id){
        if(id == null || id.isEmpty()){
            return R.error().msg("视频删除失败videoId==null");
        }
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(VodConstant.KEY_ID, VodConstant.KEY_SECRET);
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(id);
        try {
            DeleteVideoResponse response = client.getAcsResponse(request);
            log.info("RequestId = " + response.getRequestId() + "\n");
        } catch (ClientException e) {
            log.info("id:{}视频删除失败",id+e.getErrMsg());
            return R.error().msg(e.getErrMsg());
        }
        log.info("id:{}视频删除成功",id);
        return R.ok().msg("视频删除成功");
    }

    @DeleteMapping("/deleteVideoBatch")
    @ApiOperation("删除视频")
    public R deleteVideoBatch(@ApiParam( value = "云端视频ids")@RequestParam("videoIdList")  List<String> videoIdList){
        vodService.deleteVideoBatch(videoIdList);
        return R.ok();
    }

}
